/**
 * @param e 图片对象
 */
function showBigImage(e) {
    layer.open({
        type: 1,
        title: false,
        closeBtn: 0,
        shadeClose: true, //点击阴影关闭
        area: [$(e).width + 'px', $(e).height + 'px'], //宽高
        content: "<img src=" + $(e).attr('src') + " />"
    });
}

layui.use(['layer', 'form', 'table', 'admin', 'ax','element'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var element = layui.element;

    /**
     * 管理--直播课程管理
     */
    var CourseInfo = {
        tableId: "courseInfoTable",    //表格id
        condition: {
            courseInfoName: ""
        }
    };

    /**
     * 初始化表格的列
     */
    CourseInfo.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'courseName', sort: true, title: '课程名称'},
            {field: 'courseUrl', sort: true, title: '课程地址',width:180},
            {field: 'courseNums', sort: true, title: '通知次数',width:120},
            {field: 'spreadUrl', sort: true, title: '直播二维码',width:140,templet: function(d){
                    if(d.spreadUrl !=""||d.spreadUrl !=null){
                        return ' <div  ><img src="https://api.qrserver.com/v1/create-qr-code?data='+d.spreadUrl+'" alt="" width="200px" height="200px" class="layui-nav-img" onclick="showBigImage(this)"></a></div>'

                    }else{
                        return '<span style="color: #cc0604;">——</span>'
                    }
                }},
            // {field: 'status', sort: true, title: '课程状态 ',width:120,templet: function(d){
            //         if(d.status =="ENABLE"){
            //             return '<span style="color: #009688;">启用</span>'
            //         }else  if(d.status =="DISABLE"){
            //             return '<span style="color: #c2ccc6;">禁用</span>'
            //         }else{
            //             return '<span style="color: #cc0604;">——</span>'
            //         }
            //     }},
            // {field: 'noticeType', sort: true, title: '课程类型',width:120,templet: function(d){
            //         if(d.noticeType =="MAN_NOTICE"){
            //             return '<span style="color: #000000;">主动通知</span>'
            //         }else  if(d.noticeType =="SYS_NOTICE"){
            //             return '<span style="color: #f8aa14;">系统定时通知</span>'
            //         }else{
            //             return '<span style="color: #cc0604;">——</span>'
            //         }
            //     }},
            // {field: 'noticeTime', sort: true, title: '通知时间',width:160,templet: function(d){
            //         if(d.noticeType =="MAN_NOTICE"){
            //             return '<span style="color: #c2ccc6;">——</span>'
            //         }else  if(d.noticeType =="SYS_NOTICE"){
            //             return '<span style="color: #f8aa14;">'+ d.noticeTime+'</span>'
            //         }else{
            //             return '<span style="color: #c2ccc6;">——</span>'
            //         }
            //     }},
            {align: 'center', toolbar: '#tableBar', title: '操作', width:450}
        ]];
    };

    /**
     * 点击查询按钮
     */
    CourseInfo.search = function () {
        var queryData = {};
        queryData['courseInfoName'] = $("#courseInfoName").val();
        table.reload(CourseInfo.tableId, {where: queryData});
    };

    /**
     * 弹出添加直播课程
     */
    CourseInfo.openAddCourseInfo = function () {
    	/**
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加直播课程',
            content: Feng.ctxPath + '/courseInfo/courseInfo_add',
            end: function () {
                admin.getTempData('formOk') && table.reload(CourseInfo.tableId);
            }
        });
        */
        //打开新窗口形式
        window.location.href = Feng.ctxPath + '/courseInfo/courseInfo_add';
    };

    /**
     * 推送通知
     */
    CourseInfo.sendNotice = function (othis) {
        var checkRows = table.checkStatus(CourseInfo.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要推送的数据");
        } else {
            $(".layui-progress-big").show();
            CourseInfo.sendByCourseInfoId(checkRows.data.courseId);
            //触发事件
            var DISABLED = 'layui-btn-disabled';
            if(othis.hasClass(DISABLED)) return;

            //模拟loading
            var n = 0, timer = setInterval(function(){
                n = n + Math.random()*10|0;
                if(n>100){
                    n = 100;
                    clearInterval(timer);
                    othis.removeClass(DISABLED);
                    $(".layui-progress-big").hide();
                    Feng.success("通知推送成功!");
                }
                element.progress('demo', n+'%');
            }, 300+Math.random()*100);

            othis.addClass(DISABLED);
        }
    };

    //推送Htology通知
    CourseInfo.sendHtologyByCourseInfoId = function (courseId) {
        var ajax = new $ax(Feng.ctxPath + "/courseInfo/sendNoticeHtology", function (data) {
            if(data.code == '200') {
                Feng.success("通知推送成功!");
                table.reload(CourseInfo.tableId);
            }else if( data.code == '700'){
                Feng.success(data.message);
            }
        }, function (data) {

        });
        ajax.set("courseInfoId", courseId);
        ajax.start();
    }

    //推送onco通知
    CourseInfo.sendOncoByCourseInfoId = function (courseId) {
        var ajax = new $ax(Feng.ctxPath + "/courseInfo/sendNoticeOnco", function (data) {
            if(data.code == '200') {
                Feng.success("通知推送成功!");
                table.reload(CourseInfo.tableId);
            }else if( data.code == '700'){
                Feng.success(data.message);
            }
        }, function (data) {

        });
        ajax.set("courseInfoId", courseId);
        ajax.start();
    }
    /**
     * 导出excel按钮
     */
    CourseInfo.exportExcel = function () {
        var checkRows = table.checkStatus(CourseInfo.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑直播课程
     *
     * @param data 点击按钮时候的行数据
     */
    CourseInfo.onEditCourseInfo = function (data) {
    	/**
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改直播课程',
            content: Feng.ctxPath + '/courseInfo/courseInfo_edit?courseInfoId=' + data.courseId,
            end: function () {
                admin.getTempData('formOk') && table.reload(CourseInfo.tableId);
            }
        });
        */
        //打开新窗口形式
        window.location.href = Feng.ctxPath + '/courseInfo/courseInfo_edit?courseInfoId=' + data.courseId;
    };

    /**
     * 点击删除直播课程
     *
     * @param data 点击按钮时候的行数据
     */
    CourseInfo.onDeleteCourseInfo = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/courseInfo/delete", function () {
                Feng.success("删除成功!");
                table.reload(CourseInfo.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("courseInfoId", data.courseId);
            ajax.start();
        };
        Feng.confirm("是否删除直播课程【" + data.courseName + "】?", operation);
    };

    /**
     * 点击推送 Htology通知
     */
    CourseInfo.onSendNoticeHtology = function (othis,data) {
        var operation = function () {
            layer.close();
            $(".layui-progress-big").show();
            // CourseInfo.sendHtologyByCourseInfoId(data.courseId);
            //触发事件
            var DISABLED = 'layui-btn-disabled';
            if(othis.hasClass(DISABLED)) return;

            //模拟loading
            var n = 0, timer = setInterval(function(){
                n = n + Math.random()*10|0;
                if(n>100){
                    n = 100;
                    clearInterval(timer);
                    othis.removeClass(DISABLED);
                    $(".layui-progress-big").hide();
                    CourseInfo.sendHtologyByCourseInfoId(data.courseId);
                    // Feng.success("通知推送成功!");
                    // table.reload(CourseInfo.tableId);
                }
                element.progress('demo', n+'%');
            }, 30+Math.random()*100);
            othis.addClass(DISABLED);
        };
        Feng.confirm("是否将直播课程["+ data.courseName +"]通知到【Htology资讯】全体用户?", operation);
    };

    /**
     * 点击推送ONCO通知
     */
    CourseInfo.onSendNoticeOnco = function (othis,data) {
        var operation = function () {
            layer.close();
            $(".layui-progress-big").show();
            // CourseInfo.sendOncoByCourseInfoId(data.courseId);
            //触发事件
            var DISABLED = 'layui-btn-disabled';
            if(othis.hasClass(DISABLED)) return;

            //模拟loading
            var n = 0, timer = setInterval(function(){
                n = n + Math.random()*10|0;
                if(n>100){
                    n = 100;
                    clearInterval(timer);
                    othis.removeClass(DISABLED);
                    $(".layui-progress-big").hide();
                    // Feng.success("通知推送成功!");
                    CourseInfo.sendOncoByCourseInfoId(data.courseId);
                    // table.reload(CourseInfo.tableId);
                }
                element.progress('demo', n+'%');
            }, 3+Math.random()*100);
            othis.addClass(DISABLED);
        };
        Feng.confirm("是否将直播课程["+ data.courseName +"]通知到【ONCO前沿资讯】全体用户?", operation);
    };

    /**
     * 点击查看直播课程通知记录
     *
     * @param data 点击按钮时候的行数据
     */
    CourseInfo.onNoticeLog = function (data) {
        //弹窗
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            area: ['80%', '80%'],
            title: '课程通知记录',
            content: Feng.ctxPath +  '/noticeLog/byCourseId?courseInfoId=' + data.courseId,
            end: function () {
                admin.getTempData('formOk') && table.reload(CourseInfo.tableId);
            }
        });
        //打开新窗口形式
        //window.location.href = Feng.ctxPath + '/noticeLog/byCourseId?courseInfoId=' + data.courseId;
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + CourseInfo.tableId,
        url: Feng.ctxPath + '/courseInfo/list',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: CourseInfo.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        CourseInfo.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        CourseInfo.openAddCourseInfo();
    });

    // 推送通知按钮点击事件
    $('#btnNotice').click(function () {
        var othis = $(this);
        CourseInfo.sendNotice(othis);
    });
    // 导出excel
    $('#btnExp').click(function () {
        CourseInfo.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + CourseInfo.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            CourseInfo.onEditCourseInfo(data);
        } else if (layEvent === 'delete') {
            CourseInfo.onDeleteCourseInfo(data);
        }else if(layEvent ==='noticeHtology'){
            if(data.noticeType == "SYS_NOTICE"){
                Feng.info("系统定时通知会在指定时间【"+data.noticeTime+"】自动推送，无需手动触发！");
            }else{
                var othis = $(this);
                CourseInfo.onSendNoticeHtology(othis,data);
            }

        }else if(layEvent ==='noticeOnco'){
            if(data.noticeType == "SYS_NOTICE"){
                Feng.info("系统定时通知会在指定时间【"+data.noticeTime+"】自动推送，无需手动触发！");
            }else{
                var othis = $(this);
                CourseInfo.onSendNoticeOnco(othis,data);
            }

        }else if(layEvent ==='noticeLog'){
            CourseInfo.onNoticeLog(data);
        }
    });
});
